package com.itheima.springbootdemo02.controller;

import com.itheima.common.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    public Result add(){
        log.info("[用户-查询所有]……");
        // 调用service添加数据
        List<User> users =   userService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS,users);
    }

    @RequestMapping("/register")
    public Result register(String username,String password){

        //接受用户账号和密码


        //将密码 hash运算





        userService.register(username,MD5Util.md5Hex(password,"utf-8"));

        return new Result(true, MessageConst.ACTION_SUCCESS,null);
    }

    @RequestMapping("/login")
    public Result login(String username,String password){


        User user=userService.login(username,   MD5Util.md5Hex(password,"utf-8"));

        return new Result(true, MessageConst.ACTION_SUCCESS,user);
    }


    public static int myHash1(String s){

        int h = 0;
        for (int i = 0; i < s.length(); i++) {
            h = 33 * h + s.charAt(i);
        }
        return h;
    }
    public static int myHash2(String s){

        int h = 0;
        for (int i = 0; i < s.length(); i++) {
            h = 37 * h + s.charAt(i);
        }
        return h;
    }
    public static int myHash3(String s){

        int h = 0;
        for (int i = 0; i < s.length(); i++) {
            h = 101 * h + s.charAt(i);
        }
        return h;
    }

    public static void main(String[] args) {
        int code = myHash1("你好");
        //System.out.println(code);

        //不可逆特性
        //可重复计算特性

        //发明一个好的高效 碰撞率的hash算法不容易

        //md5 hash算法

        //加盐
        //

        String s = MD5Util.md5Hex("1x2i3a4o5m6i"+"xiaoming", "utf-8");

        System.out.println(s);


    }
}
